package com.asa.common.widgets;

import android.graphics.PointF;
import android.view.animation.Interpolator;

public class CubicBezierInterpolator implements Interpolator {
    private int mLastI = 0;
    private static final float STEP_SIZE = 2.4414062E-4F;
    private final PointF point1 = new PointF();
    private final PointF point2 = new PointF();

    public CubicBezierInterpolator(float x1, float y1, float x2, float y2) {
        this.point1.x = x1;
        this.point1.y = y1;
        this.point2.x = x2;
        this.point2.y = y2;
    }

    public float getInterpolation(float input) {
        float t = input;
        if (input == 0.0F) {
            this.mLastI = 0;
        }

        for(int i = this.mLastI; i < 4096; ++i) {
            t = (float)i * 2.4414062E-4F;
            double tempX = this.cubicEquation((double)t, (double)this.point1.x, (double)this.point2.x);
            if (tempX >= (double)input) {
                this.mLastI = i;
                break;
            }
        }

        double value = this.cubicEquation((double)t, (double)this.point1.y, (double)this.point2.y);
        if (input == 1.0F) {
            this.mLastI = 0;
        }

        return (float)value;
    }

    private double cubicEquation(double t, double p1, double p2) {
        double u = 1.0D - t;
        double tt = t * t;
        double uu = u * u;
        double ttt = tt * t;
        return 3.0D * uu * t * p1 + 3.0D * u * tt * p2 + ttt;
    }
}
